import { Toast } from "antd-mobile";
import axios from "axios";
import Nprogress from "nprogress";
import store from "@/store";


// 请求基地址
const baseURL = "https://smart-shop.itheima.net/index.php?s=/api";

// 创建axios实例
const request = axios.create({
   baseURL,
   timeout: 10000,
});

// 请求拦截器
request.interceptors.request.use((config) => {
   Nprogress.start();
   config.headers.Platform = "H5";
   const token:string = store.getState().loginStore.loginData.token;
   if (token) {
      config.headers['Access-Token'] = token;
   }
   Toast.show({ icon: "loading", content: "加载中…" });
   return config;
}, (error) => {
   return Promise.reject(error);
});

// 响应拦截器
request.interceptors.response.use((response) => {
   Nprogress.done();
   Toast.clear();
   return response.data;
}, (error) => {
   Nprogress.done();
   Toast.clear();
   return Promise.reject(error);
});


export default request;
